package com.weisy.signin.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.aigestudio.wheelpicker.WheelPicker;
import com.weisy.signin.R;

import java.util.List;

import androidx.annotation.NonNull;

/**
 * @author limh
 * @function
 * @date 2019/3/26 14:44
 */
public class WheelDialog extends Dialog {
    public WheelDialog(@NonNull Context context) {
        super(context, R.style.styleDialog);
    }

    public static class Builder {
        private Context context;
        private List<String> datas;
        private String left, title, right;

        private OnResultListener onResultListener;
        private DialogInterface.OnClickListener onCancelListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setDatas(List<String> datas) {
            this.datas = datas;
            return this;
        }

        public Builder setLeft(String left) {
            this.left = left;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setRight(String right) {
            this.right = right;
            return this;
        }

        public Builder setOnResultListener(Builder.OnResultListener onResultListener) {
            this.onResultListener = onResultListener;
            return this;
        }

        public Builder setOnCancelListener(OnClickListener onCancelListener) {
            this.onCancelListener = onCancelListener;
            return this;
        }

        public WheelDialog create() {

            View view = LayoutInflater.from(context).inflate(R.layout.layout_dialog_wheel, null);

            WheelDialog dialog = new WheelDialog(context);
            dialog.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            WheelPicker wheelPicker = view.findViewById(R.id.wheel_picker);
            if (null != datas && !datas.isEmpty()) {
                wheelPicker.setData(datas);
            }

            Button txtLeft = view.findViewById(R.id.btn_dg_cancel);
            Button txtTitle = view.findViewById(R.id.txt_dg_title);
            Button txtRight = view.findViewById(R.id.btn_dg_yes);
            if (!TextUtils.isEmpty(left)) {
                txtLeft.setText(left);
            }
            if (!TextUtils.isEmpty(title)) {
                txtTitle.setText(title);
            }
            if (!TextUtils.isEmpty(right)) {
                txtRight.setText(right);
            }

            txtLeft.setOnClickListener(v -> {
                if (null != onCancelListener) {
                    onCancelListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                } else {
                    dialog.dismiss();
                }
            });
            txtRight.setOnClickListener(v -> {
                if (null != onResultListener && null != wheelPicker && !wheelPicker.getData().isEmpty()) {
                    onResultListener.onResult(dialog, wheelPicker.getCurrentItemPosition());
                } else {
                    dialog.dismiss();
                }
            });
            Window window = dialog.getWindow();
            if (null != window) {
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                dialog.getWindow().setAttributes(lp);
                window.setGravity(Gravity.BOTTOM);
            }

            return dialog;
        }

        public interface OnResultListener {
            void onResult(Dialog dialog, int piosition);
        }
    }
}
